﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NursingFormSystem.Common
{
    public class EnumHelper
    {
        /// <summary>
        /// 获取指定枚举值的描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetDescription<T>(T request) where T : struct
        {
            try
            {
                Type type = request.GetType();
                FieldInfo fieldInfo = type.GetField(request.ToString());

                if (fieldInfo == null) { return string.Empty; }

                DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));

                if (da != null)
                {
                    return da.Description;
                }
                else
                {
                    return string.Empty;
                }
            }
            catch
            {
                return string.Empty;
            }
        }

    }
}
